﻿

#region Using Statements

// System References
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;

// XNA References
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

// Internal References
using Freeform.Framework;

#endregion


namespace Freeform.Framework.Input
{
    /// <summary>
    /// A class that stores a keyboard input command, its associated key and its current input state.
    /// </summary>
    public class KeyBinding
    {

        #region Events

        /// <summary>
        /// This event is fired when the key binding is pressed.
        /// </summary>
        public event EventHandler Pressed;


        /// <summary>
        /// This event is fired when the key binding is released.
        /// </summary>
        public event EventHandler Released;

        #endregion


        #region Properties

        /// <summary>
        /// Gets or sets the key that fires the input command.
        /// </summary>
        public Keys Key { get; set; }


        /// <summary>
        /// Gets or sets the name of the input command.
        /// </summary>
        public String Name { get; set; }


        /// <summary>
        /// Gets or sets the current key state of the bind.
        /// </summary>
        public KeyState State
        {
            get { return _State; }
            set
            {
                if (value == _State) return;
                _State = value;
                if (_State == KeyState.Down && Pressed != null) { Pressed(this, new EventArgs()); return; }
                if (_State == KeyState.Up && Released != null) Released(this, new EventArgs());
            }
        }
        private KeyState _State;

        #endregion


        #region Init

        /// <summary>
        /// Creates a new key binding. The key binding is automatically added to the InputManager within the constructor.
        /// </summary>
        /// <param name="Name">The name of the key.</param>
        /// <param name="Key">The bound key.</param>
        public KeyBinding(String Name, Keys Key)
        {
            this.Name = Name;
            this.Key = Key;
            this.State = Keyboard.GetState().IsKeyDown(Key) ? KeyState.Down : KeyState.Up;
            Game.GameRef.Input.Binds.Add(Name, this);
        }

        #endregion

    }
}